//
//  ScrollPageView.m
//  TVFan
//
//  Created by apple on 12-11-28.
//  Copyright (c) 2012年 apple. All rights reserved.
//

//推荐页滑动界面
#import "ScrollPageView.h"

#define TURN_PAGE_TIME 6.0f

@interface ScrollPageView(){
    CGRect _frame;
}


@end


@implementation ScrollPageView

@synthesize scroll;
@synthesize pageControl,pages;

- (id)initWithFrame:(CGRect)frame views:(NSArray *)views infos:(NSArray *)infos
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _frame=frame;
        pages=views.count;
        
    
        scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,_frame.size.width, _frame.size.height)];
        scroll.pagingEnabled = YES;
        scroll.showsHorizontalScrollIndicator = NO;
        scroll.delegate = self;
        scroll.bounces = YES;
//        scroll.backgroundColor = [UIColor redColor];
        scroll.contentSize = CGSizeMake(_frame.size.width*pages, _frame.size.height);
        [self addSubview:scroll];
        
       
        for (int i=0;i<pages;i++) {
           UIView *view = (UIView *)[views objectAtIndex:i];
           view.frame=CGRectMake(_frame.size.width*i, 0, _frame.size.width, _frame.size.height);
            [scroll addSubview:view];
        }
        
 
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,_frame.size.height-30, 320, 30)];
        imageView.backgroundColor = [UIColor blackColor];
        imageView.alpha = 0.3;
        [self addSubview:imageView];
        
        
        self.infos=infos;
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,_frame.size.height-33, 320, 30)];
        _infoLabel.textColor = [UIColor whiteColor];
        _infoLabel.textAlignment = NSTextAlignmentCenter;
		_infoLabel.font = [UIFont boldSystemFontOfSize:13.0f];
        
        if(infos.count>0)
            _infoLabel.text = [infos objectAtIndex:0];
        
		_infoLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_infoLabel];
        
        pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0,_frame.size.height-12, 320, 14)];
        pageControl.numberOfPages = pages;
        pageControl.currentPage = 0;
        pageControl.enabled=NO;
        //pageControl.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        pageControl.backgroundColor = [UIColor clearColor];
        [self  addSubview:pageControl];
        
        [self performSelector:@selector(autoTurnPage) withObject:nil afterDelay:TURN_PAGE_TIME];
    }
    return self;
}

-(void)updateWithViews:(NSArray *)views{
    for(UIView *view in scroll.subviews){
        [view removeFromSuperview];
    }
    
    pages=views.count;
    
    for (int i=0;i<pages;i++)
    {
        UIView *view = (UIView *)[views objectAtIndex:i];
        view.frame=CGRectMake(_frame.size.width*i, 0, _frame.size.width, _frame.size.height);
        [scroll addSubview:view];
    }

}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = sender.frame.size.width;
    int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
    
    if (self.infos.count>page)
    {
        NSString *info = [self.infos objectAtIndex:page];
        _infoLabel.text = info;
    }
   
    
    [self manDidTurnPage];
}

#pragma mark turnPage
-(void)autoTurnPage{
    DLog(@"pageControl.currentPage = %d",pageControl.currentPage);
    DLog(@"pageControl.numberOfPages = %d",pageControl.numberOfPages);
    if (pageControl.currentPage+1<pageControl.numberOfPages) {
        pageControl.currentPage=pageControl.currentPage+1;
    }else
        pageControl.currentPage=0;
    
    [self doTurnPage];
    
    //  [self performSelector:@selector(autoTurnPage) withObject:nil afterDelay:4.0f];
}


-(void)doTurnPage{
    int whichPage = pageControl.currentPage;
	
    _infoLabel.text = [self.infos objectAtIndex:whichPage];
    
    if (whichPage!=0) {
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3f];
        scroll.contentOffset = CGPointMake(320.0f * whichPage,0.0f);
        [UIView commitAnimations];
    }
    else {
        [scroll setContentOffset:CGPointMake(0,0) animated:NO];
    }
    
    
}


-(void)manDidTurnPage{
    [ScrollPageView cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoTurnPage) object:nil];
    [self performSelector:@selector(autoTurnPage) withObject:nil afterDelay:TURN_PAGE_TIME];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
